
-module(chat_private_test).

-include_lib("eunit/include/eunit.hrl").

-export([run_tests/0]).

-define(ROLE1, 1111).
-define(ROLE2, 2221).

run_tests() ->
    ?debugMsg("chat_private_test start --"),
    chat:start(),
    Pid1 = spawn_link(fun role1/0),
    Pid2 = spawn_link(fun role2/0),
    Pid1 ! "I'm ready!",
    Pid2 ! "I'm ready!",

    MsgAmount = 15,
    MsgList1 = [{Id, integer_to_list(?ROLE1) ++ " msg " ++ integer_to_list(Id)} || Id <- lists:seq(1, MsgAmount)],
    MsgList2 = [{Id, integer_to_list(?ROLE2) ++ " msg " ++ integer_to_list(Id)} || Id <- lists:seq(1, MsgAmount)],

    chat:private_login(?ROLE1, Pid1),
    [chat:private_talk(?ROLE1, Msg) || Msg <- MsgList1],

    chat:private_login(?ROLE2, Pid2),
    [chat:private_talk(?ROLE2, Msg) || Msg <- MsgList2],

    ?debugVal(chat:private_fetch(?ROLE1, 0)),
    ?debugVal(chat:private_fetch(?ROLE1, 5)),

    {ok, CPW1} = chat_private_sup:find_child(1),
    ?debugVal(erlang:process_info( CPW1 )),

    close("no process exit"),
    close("no process exit"),
    ?debugMsg("chat_private_test over"),
    ok.




role1() -> loop(?ROLE1).
role2() -> loop(?ROLE2).

loop(RoleId) ->
    receive
        Msg ->
            ?debugFmt("role: ~p, Msg: ~p", [RoleId, Msg]),
            loop(RoleId)
    after
        1000 ->
            ?debugFmt("recevie process exit, role: ~p, pid: ~p", [RoleId, self()]),
            exit({timeout, RoleId})
    end.


close(Msg) ->
    receive
        Recv ->
            ?debugFmt("process ~p exit msg", [Recv])
    after
        5000 ->
            ?debugMsg(Msg)
    end.

